package com.pretty.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pretty.test.controller.system.dict.dto.*;
import com.pretty.test.controller.system.dict.vo.*;
import com.pretty.test.entity.PetsDict;
import com.pretty.test.mapper.PetsDictMapper;
import com.pretty.test.service.IPetsDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pretty.test.utils.otherutils.Func;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统字典服务实现类
 * @author dujianyu
 * @since 2021-03-31
 */
@Service
@AllArgsConstructor
public class PetsDictServiceImpl extends ServiceImpl<PetsDictMapper, PetsDict> implements IPetsDictService {


    @Override
    public IPage<PetsDictVO> selectDictList(Page<PetsDictVO> page, PetsDictSearchDTO searchDTO) {

        return baseMapper.selectDictList(page,searchDTO);
    }

    @Override
    public IPage<ChildrenPetsDictVO> selectChildrenDictList(Page<ChildrenPetsDictVO> page, ChildrenPetsDictSearchDTO searchDTO) {
        return baseMapper.selectChildrenDictList(page,searchDTO);
    }

    @Override
    public Boolean submit(PetsDict petsDict) {
        if(Func.isEmpty(petsDict.getId())){
            return baseMapper.insert(petsDict) == 1;
        }else{
            if(petsDict.getParentId() == 0){
                baseMapper.childrenDictCodeUpdate(petsDict);
            }
            return baseMapper.updateById(petsDict) == 1;
        }
    }

    @Override
    public Boolean changeIsRun(PetsDict petsDict) {
        return baseMapper.updateById(petsDict) == 1;
    }

    @Override
    public List<DictKeyValueVO> dictList(String code) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_deleted",0);
        queryWrapper.eq("code",code);
        queryWrapper.ne("parent_id",0);
        queryWrapper.orderByAsc("dict_key");
        List<PetsDict> list = baseMapper.selectList(queryWrapper);
        List<DictKeyValueVO> vo = new ArrayList<>();
        for(PetsDict item: list){
            DictKeyValueVO temp = new DictKeyValueVO();
            temp.setDictKey(item.getDictKey());
            temp.setDictValue(item.getDictValue());
            vo.add(temp);
        }
        return vo;
    }

    @Override
    public Boolean delete(String id) {
        return baseMapper.deleteById(id) == 1;
    }


}
